package com.sakura.mall.coupon.coupon.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.coupon.domain.entity.CouponEntity;
import com.sakura.mall.coupon.coupon.domain.request.AddCouponRequest;
import com.sakura.mall.coupon.coupon.domain.request.UpdateCouponRequest;
import com.sakura.mall.coupon.coupon.domain.request.CouponPageRequest;
import com.sakura.mall.coupon.coupon.domain.vo.CouponVo;
import com.sakura.mall.coupon.coupon.mapper.CouponMapper;
import com.sakura.mall.coupon.coupon.service.CouponService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponEntity> implements CouponService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCouponRequest request) {
        // todo 其他校验逻辑
        CouponEntity coupon = request.convert(CouponEntity.class);
        this.save(coupon);
    }

    @Override
    public Page<CouponVo> queryPage(CouponPageRequest request) {
        IPage<CouponEntity> qp = request.toPage();
        LambdaQueryWrapper<CouponEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CouponVo> ret = qp.convert(entity -> entity.convert(CouponVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCouponRequest request) {
        CouponEntity entity = request.convert(CouponEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

